package cn.cboard.repository;

import cn.cboard.dao.UserDao;
import cn.cboard.model.Authorization;
import cn.cboard.model.User;

public class UserFactory {
	private UserDao userDao;
	public UserFactory(UserDao userDao){
		this.userDao = userDao;
	}
	public User getFullUser(User userIn){
		if(userIn == null){
			return createAnonymous();
		}
		
		User user = null;
		
		if(userIn.getUserId() != null){
			user = userDao.getUser(userIn.getUserId());
		}
		if(userIn.getUsername() != null){
			user = userDao.getUserByName(userIn.getUsername(),userIn.getPassword());
		}
		if(user==null){
			user = createAnonymous();
		}
		return user;
	}
	
	public static Authorization getAnonymousAuthorization(){
		return new Authorization(new Long(-1));
	}
	public Authorization getAuthorization(String username,String password){
		User u = userDao.getUserByName(username,password);
		if(u==null)
			return getAnonymousAuthorization();
		else
			return new Authorization(u.getUserId());
	}
	private User createAnonymous(){
		User user = new User();
		user.setAnonymous(true);
		user.setName("Anonymous");
		user.setPassword("anonymous@anonymouse.com");
		user.setSignature("Anonymouse Signature");
		user.setTopicCount(new Long(0));
		user.setUsername("anonymous");
		user.setUserId(new Long(0));
		return user;
	}

}
